import useDictStore from '@/store/modules/dict'
import apiDictData from '@/api/system/dictData'

/**
 * 获取字典数据
 */
export function useDict(...args:string []) {
  const res:any = ref({});
  return (() => {
    args.forEach((dictCode:any) => {
      res.value[dictCode] = [];
      const dicts = useDictStore().getDict(dictCode);
      if (dicts) {
        res.value[dictCode] = dicts;
      } else {
        apiDictData.getByDictCode(dictCode).then(resp => {
          res.value[dictCode] = resp.data.map((dict:any) => ({ ...dict }))
          useDictStore().setDict(dictCode, res.value[dictCode]);
        })
      }
    })
    return toRefs(res.value);
  })()
}
